using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
using LS.Tools;

namespace LS.GameSystem
{
    public sealed class GameSystemComponent : GameFrameworkComponent
    {
        private GameSystemManager m_SystemManager = null;
        private TimeUpdater m_TimeUpdater = null;
        private TimeUpdater m_FixedTimeUpdater = null;
        protected override void Awake()
        {
            base.Awake();
            m_SystemManager = new GameSystemManager();
            m_TimeUpdater = new TimeUpdater();
            m_TimeUpdater.OnTimeUpdate += m_SystemManager.Update;
            m_TimeUpdater.OnTimeSecondsUpdate += m_SystemManager.UpdateSeconds;
            m_TimeUpdater.OnRealTimeSecondsUpdate += m_SystemManager.UpdateRealSeconds;

            m_FixedTimeUpdater = new TimeUpdater();
            m_FixedTimeUpdater.OnTimeUpdate += m_SystemManager.FixedUpdate;
        }

        private void Update()
        {
            m_TimeUpdater?.Update(Time.deltaTime, Time.unscaledDeltaTime);
        }

        private void OnDestroy()
        {
            m_TimeUpdater.Clear();
            m_TimeUpdater = null;

            m_SystemManager?.Shutdown();
            m_SystemManager = null;
        }

        /// <summary>
        /// 注册控制器
        /// </summary>
        /// <param name="system"></param>
        public void RegisterSystem(GameSystemBase system)
        {
            m_SystemManager.RegisterSystem(system);
        }

        public T RegisterSystem<T>(string systemName) where T : GameSystemBase, new()
        {
            return m_SystemManager.RegisterSystem<T>(systemName);
        }

        public GameSystemBase RegisterSystem(Type controllerType, string systemName)
        {
            return m_SystemManager.RegisterSystem(controllerType, systemName);
        }

        /// <summary>
        /// 移除控制器
        /// </summary>
        /// <param name="controller"></param>
        public void RemoveSystem(GameSystemBase controller)
        {
            m_SystemManager.RemoveSystem(controller);
        }

        public void RemoveSystem<T>(string systemName) where T : GameSystemBase, new()
        {
            m_SystemManager.RemoveSystem<T>(systemName);
        }

        public void RemoveSystem(Type controllerType, string systemName)
        {
            m_SystemManager.RemoveSystem(controllerType, name);
        }

        public bool HasSystem(GameSystemBase controller)
        {
            return m_SystemManager.HasSystem(controller);
        }

        public bool HasSystem<T>(string systemName) where T : GameSystemBase, new()
        {
            return m_SystemManager.HasSystem<T>(systemName);
        }

        /// <summary>
        /// 获取控制器
        /// </summary>
        /// <param name="systenType"></param>
        /// <param name="systemName"></param>
        /// <returns></returns>
        public GameSystemBase GetSystem(Type systenType, string systemName)
        {
            return m_SystemManager.GetSystem(systenType, systemName);
        }

        public T GetSystem<T>(string systemName) where T : GameSystemBase, new()
        {
            return m_SystemManager.GetSystem<T>(systemName);
        }
        
        public GameSystemBase[] GetAllSystem()
        {
            return m_SystemManager.GetAllSystems();
        }
    }
}
